package edu.kit.aifb.ldstream.repository.listeners;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFParseException;

import edu.kit.aifb.ldstream.env.Environment;

/**
 * Listener thread which listens to a N3-file and save the content to the global
 * repository.
 */

public class FileListeningThread extends AbstractListeningThread {

	private String filePath;
	private RDFFormat format;


	public FileListeningThread(String filePath, String baseURI){
		super(baseURI);
		this.filePath = filePath;
		this.format = getRDFFormat(filePath);
	}

	/**
	 * 
	 * @param filePath
	 *            of the N3 formatted file.
	 * @param baseURI
	 *            of the file.
	 */
	public FileListeningThread(String filePath, String baseURI, RDFFormat format) {
		super(baseURI);
		this.filePath = filePath;
		this.format = format;
	}

	public void run() {
		try {
			RepositoryConnection con = Environment.REPO.getConnection();
			try {
				if(this.format != null){

					if (Environment.VERBOSE) {
						System.out.println("Reading file " + filePath);
					}
					con.add(new FileReader(new File(filePath)), this.baseURI,
							this.format, Environment.STREAM_CONTEXT_URI);
					/*
					con.add(new FileReader(new File(filePath)), this.baseURI,
							this.format, con.getValueFactory().createURI(this.baseURI + "#" + getFileName()));
					*/
					if (Environment.VERBOSE) {
						System.out.println("File " + filePath
								+ " added to the repository");
					}
				}
			} catch (RDFParseException e) {
				System.err
				.println("The given file containes RDF syntax errors");
			} catch (FileNotFoundException e) {
				System.err.println("The given file is not accessible");
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				con.commit();
				con.close();
			}
		} catch (RepositoryException e) {
			e.printStackTrace();
		}
	}

	private RDFFormat getRDFFormat(String fileName){
		int pos = fileName.lastIndexOf(".");
		String extension = fileName.substring(pos);

		if(extension.equals(".n3")){
			return RDFFormat.N3;
		}
		if(extension.equals(".nt")){
			return RDFFormat.NTRIPLES;
		}
		if(extension.equals(".trix")){
			return RDFFormat.TRIX;
		}
		if(extension.equals(".rdf")){
			return RDFFormat.RDFXML;
		}
		if(extension.equals(".ttl")){
			return RDFFormat.TURTLE;
		}
		if(extension.equals(".trig")){
			return RDFFormat.TRIG;
		}
		return null;
	}

	private String getFileName(){
		String s;
		int pos = this.filePath.lastIndexOf("/");
		if( pos != -1){
			s = this.filePath.substring(pos + 1);
		}else{
			pos = this.filePath.lastIndexOf("\\");
			s = this.filePath.substring(pos + 1);
		}

		return s;
	}

}
